var httpGoods = require('../../utils/goods.js');
var httpStore = require('../../utils/store.js');
var pageClass = require('../../library/page.js');

var config = new pageClass({
  //导航列表
  columns: [
    {
      label   : '今日推荐',
      selected: true,
      template: 'goodsList'
    }, {
      label   : '精品服务',
      selected: false,
      template: 'goodsList'
    }, {
      label   : '特惠商品',
      selected: false,
      template: 'goodsList',
      loader  : 'loaderGoodsList'
    }, {
      label   : '小店简介',
      selected: false,
      template: 'storeInfo',
      loader  : 'loaderStoreInfo'
    }
  ],
  //横幅轮播图片列表
  bannerList: [
    { "banner_img": "https://luckyxiaopang.com/blueblue/images/banner-1.jpg" },
    { "banner_img": "https://luckyxiaopang.com/blueblue/images/banner-2.jpg" },
    { "banner_img": "https://luckyxiaopang.com/blueblue/images/banner-3.jpg" },
    { "banner_img": "https://luckyxiaopang.com/blueblue/images/banner-4.jpg" },
    { "banner_img": "https://luckyxiaopang.com/blueblue/images/banner-5.jpg" }
  ]
});

/**
 * 加载商品列表方法
 */
config.loaderGoodsList = function(config){
  let http = new httpGoods;
  let self = this;
  http.success = function (data) {
    config.data = data.list;
    self.setData(self.data);
  };
  http.send();
};

/**
 * 加载门店信息方法
 */
config.loaderStoreInfo = function (config) {
  let http = new httpStore;
  let self = this;
  http.success = function (data) {
    config.data = data;
    self.setData(self.data);
  };
  http.send();
};


/**
 * 页面载入事件 
 */
config.onLoad = function(){
  let self = this;
  wx.getSystemInfo({
    success: function (res) {
      let base = res.windowWidth/750;
      let width = base*420; 
      self.setData({
        contentHeight: res.windowHeight - width
      });
    }
  });
};

/**
 * 导航栏切换事件
 */
config.jumpNavigation = function(event){
  for (let index in this.data.columns)
    this.data.columns[index].selected=false;
  var index = parseInt(event.target.dataset.index);
  this.data.columns[index].selected = true;
  this.setData(this.data);
  if ('data' in this.data.columns[index]) return;
  if ('loader' in this.data.columns[index]){
    let loader = this.data.columns[index].loader;
    this[loader](this.data.columns[index]);
  }
};

/**
 * 链接点击事件
 */
config.bindViewTap = function (event) {
  console.log(event.currentTarget.dataset.link);
  wx.navigateTo({ url: event.currentTarget.dataset.link});
  //wx.navigateTo({ url: '../goods/index'});
};

Page(config);